class ZjlyPrice < ActiveRecord::Base
  belongs_to :from_airport, :class_name => 'Airport', :foreign_key => 'from_id'
  belongs_to :mid_airport, :class_name => 'Airport', :foreign_key => 'mid_id'
  belongs_to :mid_airport2, :class_name => 'Airport', :foreign_key => 'mid_id2'
  belongs_to :to_airport, :class_name => 'Airport', :foreign_key => 'to_id'
  belongs_to :mid_airport3, :class_name => 'Airport', :foreign_key => 'mid_id3'
  belongs_to :mid_airport4, :class_name => 'Airport', :foreign_key => 'mid_id4'
  belongs_to :end_airport, :class_name => 'Airport', :foreign_key => 'end_id'

  belongs_to :airways
  
  def self.saveZjlyPrice(id,from_id,mid_id,mid_id2,to_id,mid_id3,mid_id4,end_id,airways_id,start_date,end_date,price,position,position2,position3,position4,trip_type,rake_off,is_closed,policy)
    begin
      zjly = ZjlyPrice.find_by_id(id) if id
      zjly = ZjlyPrice.new() if zjly==nil
      
      zjly.from_id = from_id.to_i if from_id && from_id.to_i>0
      zjly.mid_id = mid_id.to_i if mid_id && mid_id.to_i>0
      zjly.mid_id2 = mid_id2.to_i if mid_id2 && mid_id2.to_i>0
      zjly.to_id = to_id.to_i if to_id && to_id.to_i>0
      zjly.mid_id3 = mid_id3.to_i if mid_id3 && mid_id3.to_i>0
      zjly.mid_id4 = mid_id4.to_i if mid_id4 && mid_id4.to_i>0
      zjly.end_id = end_id.to_i if end_id && end_id.to_i>0
      if airways_id && airways_id.to_i>0
        airways = Airways.find_by_id(airways_id)
        zjly.airways_code = airways.airways_code if airways
        zjly.airways_id = airways_id.to_i
      end
      zjly.start_date = start_date if start_date && start_date!=""
      zjly.end_date = end_date if end_date && end_date!=""
      zjly.price = price.to_i if price
      zjly.position = position if position
      zjly.position2 = position2 if position2
      zjly.position3 = position3 if position3
      zjly.position4 = position4 if position4
      zjly.trip_type = trip_type if trip_type
      zjly.rake_off = rake_off.to_i if rake_off
      zjly.is_closed = is_closed.to_i if is_closed
      zjly.policy = policy if policy
      if zjly.save
        return zjly.id
      end
      return nil
    rescue Exception => e
      logger.error("model ZjlyPrice saveZjlyPrice e="+e.message)
      return nil
    end
  end
  
  
end
